// pages/energylist/energylist.js
const app = getApp();
var router = require("../../router.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [  
    ],
    page: 0,
    info: false, 
  },
  start_energy: function (e) {
    var id = e.currentTarget.dataset.id;   
    app.navTo('../listens/listens?id=' + id)
  },
  lower: function (e) {
    this.getInfo();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      height: wx.getSystemInfoSync().windowHeight
    });  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo();
  },

  getInfo: function () {
    var that = this;
    app.request({
      url: router.msg.index,
      data: {
        page: that.data.page
      },
      success: function (data) {       
        if (data.length > 0) {
          for (var i = 0; i < data.length; i++) {
            var obj = {};     
            that.data.list.push(data[i]);
          }
          that.setData({
            list: that.data.list,
            page: that.data.page + 1
          });
        } else {
          that.setData({
            info: true,
          });
        }
      }
    })
  },
  //分享
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      var that = this;
      // 来自页面内转发按钮    
      var id = res.target.dataset.id;
      var index = res.target.dataset.ctr;
      var title = that.data.list[index].name;
      return {
        title: title,
        path: '/pages/energylist/energylist',
        success: function (res) {
          app.request({
            url: router.msg.share,
            method: "POST",
            data: {
              id: id,
            },
            success: function (data) {
              if (data) {
                var list = that.data.list;
                var share = list[index].share;
                list[index].share = share + 1;
                that.setData({
                  list: list
                })
                wx.showToast({
                  title: '分享成功',
                  icon: 'success',
                  duration: 2000
                });
              } else {
                wx.showToast({
                  title: '分享失败请重试',
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          });
        },
        fail: function (res) {
          wx.showToast({
            title: '分享已取消',
            icon: 'none',
            duration: 2000
          });
        }
      }
    }
  },
})